/**
 * @author vidy3587@gmail.com
 */
import axios from "axios";
import qs from "qs";
import { Notify } from 'vant';
console.log("process.env.VUE_APP_API", process.env.VUE_APP_API);
export const httpClient = axios.create({
    baseURL: process.env.VUE_APP_API,
    timeout: 1000 * 30,
    headers: {
        // 'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    },
    onUploadProgress(ev) {
        console.log('on http upload progress', ev);
    },
    onDownloadProgress(ev) {
        console.log('on download progress', ev);
    },
    transformRequest: [(data, headers) => {
        console.log(data, headers);
        if (headers['Content-Type'] === "application/json") {
            return JSON.stringify(data);
        } else {
            return qs.stringify(data)
        }
    }],
    // `transformResponse` allows changes to the response data to be made before
    // it is passed to then/catch
    transformResponse: [function (response) {
        // Do whatever you want to transform the data
        console.log('response data is ', response);
        return JSON.parse(response);
    }],
    validateStatus: function (status) {
        return status >= 200 && status < 300; // default
    },
    // `cancelToken` specifies a cancel token that can be used to cancel the request
    // (see Cancellation section below for details)
    // cancelToken: new CancelToken(function (cancel) {
    // }),
});

httpClient.interceptors.response.use((r) => {
    if (r.data.status === 0) {
        return r.data;
    }
    Notify(r.data.message);
    return Promise.reject(r.data.message || r.data.detailMsg);
}, (e) => {
    console.error("http error", e);
    return Promise.reject(e);
});